package teacher.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import teacher.bean.Award;
import teacher.service.award;
import tools.StringDate;

import com.opensymphony.xwork2.ActionSupport;

public class modifyAwa2 extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private File fileCom;
	private String fileComFileName;
	private String uploadPath;
	private String FilePath;
	
	private Award awar;
	private int awaid;
	private int teaid;
	private String upname;
	private String dates;
	String suc;

	public String getUpname() {
		return upname;
	}

	public void setUpname(String upname) {
		this.upname = upname;
	}

	public File getFileCom() {
		return fileCom;
	}

	public void setFileCom(File fileCom) {
		this.fileCom = fileCom;
	}

	public String getFileComFileName() {
		return fileComFileName;
	}

	public void setFileComFileName(String fileComFileName) {
		this.fileComFileName = fileComFileName;
	}

	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}

	public int getTeaid() {
		return teaid;
	}

	public void setTeaid(int teaid) {
		this.teaid = teaid;
	}

	public String getDates() {
		return dates;
	}

	public void setDates(String dates) {
		this.dates = dates;
	}
	
	public Award getAwar() {
		return awar;
	}

	public void setAwar(Award awar) {
		this.awar = awar;
	}

	public int getAwaid() {
		return awaid;
	}

	public void setAwaid(int awaid) {
		this.awaid = awaid;
	}

	public String execute() throws Exception{
		
		if(fileCom!=null)
		{
			InputStream is = new FileInputStream(fileCom);

			uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");

			File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
	            FilePath=uploadPath+File.separator+this.getFileComFileName();

	     	    File toFile = new File(uploadPath, this.getFileComFileName());

	     		OutputStream os = new FileOutputStream(toFile);

	     		byte[] buffer = new byte[4096];

	     		int length = 0;

	     		while ((length = is.read(buffer)) > 0) {
	     			os.write(buffer, 0, length);
	     		}
	     		System.out.println("上传文件名"+fileComFileName);
	     		System.out.println(FilePath);

	     		is.close();

	     		os.close();
			
	     		awar.setUpload(fileComFileName);
		}else{
			awar.setUpload(upname);
		}
		
	
		
		awar.setAwaId(awaid);
		awar.setState(0);
		awar.setTeaId(teaid);
		Date d=new StringDate().StringToDate(dates);
		awar.setDate(d);
	
		if(new award().updateAward(awar, awaid)){
			this.setSuc("success2");
			return SUCCESS;
		}
		return ERROR;
	}

}
